package drops.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.List;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.*;

import drops.control.PatientControl;
import drops.dao.DentalTransaction;
import drops.dao.DentalTransactionPeer;
import drops.dao.Patient;

public class DentalRecordPanel extends JPanel implements MouseListener, ActionListener{
	DentalDiagramPanel dentalDiagramPanel;
	JPanel patientInfoPanel;
	JPanel transactionTablePanel;
	
	JLabel firstNameLabel;
	JLabel middleNameLabel;
	JLabel lastNameLabel;
	JLabel addressLabel;
	JLabel contactNumberLabel;
	JLabel ageLabel;	
	JLabel occupationLabel;	
	JLabel statusLabel;
	JLabel complainLabel;

	JLabel rightLabel;
	JLabel leftLabel;
	JLabel upperLabel;
	JLabel lowerLabel;
	
	JLabel[] upperALabels;
	JLabel[] upperBLabels;
	JLabel[] lowerALabels;
	JLabel[] lowerBLabels;
	
	ImageIcon[] upperAIcons;
	ImageIcon[] upperBIcons;
	ImageIcon[] lowerAIcons;
	ImageIcon[] lowerBIcons;
	
	JTextField firstNameField;
	JTextField middleNameField;
	JTextField lastNameField;
	JTextField addressField;
	JTextField contactNumberField;
	JFormattedTextField ageField;	
	JTextField occupationField;	
	JComboBox statusField;
	JTextField complainField;
	ComboBoxModel statusModel;
	
	JTable transactionTable;
	
	JButton editButton;
	JButton submitButton;
	
	Vector transactionHeader;
	Vector transactionData;

	JButton addTransactionButton;
	JButton deleteTransactionButton;
	List <DentalTransaction> transactionList;
	DefaultTableModel transactionModel;
	
	int patientNumber;
	MainFrame mainFrame;
	JPanel transactionHeaderPanel;
	SearchPanel searchPanel;
	
	
	public DentalRecordPanel(MainFrame mainFrame){
		
		this.mainFrame = mainFrame;
		searchPanel = new SearchPanel(mainFrame);
		
		setLayout(new BorderLayout());
		
		dentalDiagramPanel = new DentalDiagramPanel(this);
		patientInfoPanel = new JPanel();
		transactionTablePanel = new JPanel();

		patientInfoPanel.setLayout(new GridLayout(10,2));
		dentalDiagramPanel.setLayout(new GridLayout(4,16));
		
		
		
		/**************************/
		/*****DENTAL  DIAGRAM******/
		/**************************/
		
   		
		/**************************/
		/******PATIENT  INFO*******/
		/**************************/
		
		editButton = new JButton("Edit");
		editButton.addActionListener(this);
		submitButton = new JButton("Submit");
		submitButton.addActionListener(this);
		
		firstNameLabel = new JLabel("First Name:");
		middleNameLabel = new JLabel("Middle Name:");
		lastNameLabel = new JLabel("Last Name:");
		addressLabel = new JLabel("Address:");
		contactNumberLabel = new JLabel("Contact Number:");
		ageLabel = new JLabel("Age:");
		occupationLabel = new JLabel("Occupation:");
		statusLabel = new JLabel("Status:");
		complainLabel = new JLabel("Complain:");
		
		firstNameField = new JTextField(15);
		middleNameField = new JTextField(15);
		lastNameField = new JTextField(15);
		addressField = new JTextField(15);
		contactNumberField = new JTextField(15);
		ageField = new JFormattedTextField(NumberFormat.getIntegerInstance());
		occupationField = new JTextField(15);
		String [] statusChoices = {"Single", "Married", "Divorced", "Widowed"};
		statusModel = new DefaultComboBoxModel(statusChoices);
		statusField = new JComboBox(statusModel);
		complainField = new JTextField(15);
		
		patientInfoPanel.add(firstNameLabel);
		patientInfoPanel.add(firstNameField);
		firstNameField.setEditable(false);
			
		patientInfoPanel.add(middleNameLabel);
		patientInfoPanel.add(middleNameField);
		middleNameField.setEditable(false);
		
		patientInfoPanel.add(lastNameLabel);
		patientInfoPanel.add(lastNameField);
		lastNameField.setEditable(false);
		
		patientInfoPanel.add(addressLabel);
		patientInfoPanel.add(addressField);
		addressField.setEditable(false);
		
		patientInfoPanel.add(contactNumberLabel);
		patientInfoPanel.add(contactNumberField);
		contactNumberField.setEditable(false);
   		
   		patientInfoPanel.add(ageLabel);
   		patientInfoPanel.add(ageField);
   		ageField.setEditable(false);
   		
   		patientInfoPanel.add(occupationLabel);
   		patientInfoPanel.add(occupationField);
   		occupationField.setEditable(false);
   		
   		patientInfoPanel.add(statusLabel);
   		patientInfoPanel.add(statusField);
   		statusField.setEnabled(false);
   		
   		patientInfoPanel.add(complainLabel);
   		patientInfoPanel.add(complainField);
   		complainField.setEditable(false);
   		
   		patientInfoPanel.add(editButton);
   		
		
	       
		
		
   		
		/**************************/
		/****TRANSACTION TABLE*****/
		/**************************/		
		
		transactionTable = new JTable();
		
		transactionHeader = new Vector();
    	transactionData = new Vector(); 
		
		Dimension d = new Dimension(800,300);
		
		transactionHeader.add("#");
		transactionHeader.add("Date");
        transactionHeader.add("Description");
        transactionHeader.add("Time"); 
 		transactionHeader.add("Debit");
 		transactionHeader.add("Credit Date"); 
 		transactionHeader.add("Credit Amount");
 		transactionHeader.add("Balance");
      	
      	transactionModel = new DefaultTableModel(transactionData, transactionHeader) {
      		public boolean isCellEditable(int row, int col) {
      			return false;
      		}
      	};
      	
      	transactionTable = new JTable(transactionModel);
    	
      	
      	transactionTable.setPreferredScrollableViewportSize(d); 
      	transactionTable.setFillsViewportHeight(false);
      	transactionTable.getTableHeader().setReorderingAllowed(false);
    	JScrollPane scrollPane = new JScrollPane(transactionTable);
		
    	transactionTable.setRowHeight(50);

    	transactionHeaderPanel = new JPanel();
    	
    	addTransactionButton = new JButton("Add Transaction");
    	addTransactionButton.addActionListener(this);
    	deleteTransactionButton = new JButton("Delete");
    	deleteTransactionButton.addActionListener(this);
    	transactionHeaderPanel.add(addTransactionButton);
    	transactionHeaderPanel.add(deleteTransactionButton);
    	transactionTablePanel.setLayout(new BorderLayout());
    	transactionTablePanel.add(transactionHeaderPanel,BorderLayout.NORTH);
    	transactionTablePanel.add(scrollPane, BorderLayout.CENTER);
		transactionTable.addMouseListener(this);

   	  	add(dentalDiagramPanel, BorderLayout.WEST);
    	add(patientInfoPanel, BorderLayout.EAST);
	  	add(transactionTablePanel, BorderLayout.SOUTH); 
	  	
	}
	
	public void deleteTransaction() {
		int row = transactionTable.getSelectedRow();
		if (row==-1) return;
		int transactionNumber = Integer.parseInt(transactionModel.getValueAt(row, 0).toString());
		DentalTransactionPeer.deleteDentalTransaction(transactionNumber);
		transactionModel.removeRow(row);
	}
	
    
	 public void actionPerformed(ActionEvent e) {
		 if (e.getSource()==editButton) { 
			 if(editButton.getText()=="Edit"){
     			
     			firstNameField.setEditable(true);
     			middleNameField.setEditable(true);
     			lastNameField.setEditable(true);
     			addressField.setEditable(true);
     			contactNumberField.setEditable(true);
     			ageField.setEditable(true);
     			occupationField.setEditable(true);
     			statusField.setEnabled(true);
     			complainField.setEditable(true);

     			editButton.setText("Submit");
     	
     		}
     		else if(editButton.getText()=="Submit"){
     		
     			firstNameField.setEditable(false);
	        	middleNameField.setEditable(false);
	        	lastNameField.setEditable(false);
	        	addressField.setEditable(false);
	        	contactNumberField.setEditable(false);
	        	ageField.setEditable(false);
	        	occupationField.setEditable(false);
	        	statusField.setEnabled(false);
	        	complainField.setEditable(false);
	        	
	        	if (PatientControl.editPatient(patientNumber, firstNameField.getText(), middleNameField.getText(), lastNameField.getText(), addressField.getText(), contactNumberField.getText(), Integer.parseInt(ageField.getValue().toString()), occupationField.getText(), statusField.getSelectedItem().toString(), complainField.getText())) {
		        	JOptionPane.showMessageDialog(this, "Edit Patient successful");
		        	mainFrame.refreshPatients();
		      		editButton.setText("Edit");
	        	}
	        	else {
	        		JOptionPane.showMessageDialog(this, "Edit Patient failed");
	        	}
     		}
		 }
     	else if (e.getSource()==addTransactionButton)  {
			mainFrame.addTransactionFrame();
			mainFrame.setPatient(patientNumber);
		}
     	else if (e.getSource()==deleteTransactionButton){
     		deleteTransaction();
     	}
 }

	public void mouseClicked(MouseEvent arg0) {
		Object o = arg0.getSource();
		if (o == transactionTable) {
			if (arg0.getClickCount()==2&&transactionTable.getSelectedRow()>=0) {
				mainFrame.editTransactionFrame();
				mainFrame.setTransaction(Integer.parseInt(transactionModel.getValueAt(transactionTable.getSelectedRow(), 0).toString()));
			}
		}
	}
	
	public void updateTransactions() {
		transactionList = DentalTransactionPeer.searchTransactionByPatientNumber(patientNumber);
		transactionModel.setRowCount(0);
		DateFormat df = DateFormat.getDateInstance();
		DateFormat tf = DateFormat.getTimeInstance();
		for (int i=0, size=transactionList.size();i<size;i++) {
			Vector rowData = new Vector();
			DentalTransaction t = transactionList.get(i);
			rowData.add(t.getTransactionNumber());
			rowData.add(df.format(t.getDate()));
			rowData.add(t.getDescription());
			rowData.add(tf.format(t.getDate()));
			rowData.add(t.getDebit());
			rowData.add(df.format(t.getCreditDate()));
			rowData.add(t.getCreditAmount());
			rowData.add(t.getBalance());
			transactionModel.addRow(rowData);
		}
	}

	public void refresh() {
		updateTransactions();
	}

	public void setPatient(int patientNumber) {
		Patient p = PatientControl.searchPatientByNumber(patientNumber);
		this.patientNumber = patientNumber;
		firstNameField.setText(p.getPatientFirstName());
		middleNameField.setText(p.getPatientMiddleName());
		lastNameField.setText(p.getPatientLastName());
		addressField.setText(p.getAddress());
		contactNumberField.setText(p.getContactNumber());
		ageField.setValue(p.getAge());
		occupationField.setText(p.getOccupation());
		statusField.setSelectedItem(p.getStatus());
		complainField.setText(p.getComplain());
		dentalDiagramPanel.setPatient(patientNumber);
		updateTransactions();
	}
	
		public void mouseEntered(MouseEvent arg0) {
	        }
	
	        public void mouseExited(MouseEvent arg0) {
	        }

	        public void mousePressed(MouseEvent arg0) {

	        }

	        public void mouseReleased(MouseEvent arg0) {
	        }
}
